var dir = '';
var code = '';
function onload() {
    dir = getArgs('dir');
    code = getArgs('code');
    loadSvnDir(dir, code)
}

function loadSvnDir(dir, code) {
// 加载配置文件详情
    var configJSON = 'config/config.json';
    $.getJSON(configJSON, function (data) {

        if((code == null || code.length < 1) || (dir == null || dir.length < 1)){
            alert("未找到该页面");
            window.history.back(-1);
        }
        var htmldata = '';
        var htmltitle = '';
        var backAction =  '<i class="material-icons back-icon" onclick="location.href=\'list.html?code='+ code + '\'">返回 reply</a></i>';
        $("#back-action").html(backAction);
        $("#card-action").html("目录读取中，请稍后……");
        $("#collection").html(htmldata);
        $.each(data,function (i, item) {
            if(item.code == code) {
                $.each(item.data,function (j, row) {
                    if(row.name == dir) {
                        htmltitle = row.desc;
                        $.ajax({
                            type: 'post',
                            url: '/svnportal/svn/browser-out/',
                            contentType: "application/json; charset=utf-8",
                            dataType: 'json',
                            data: JSON.stringify({
                                path: row.url
                            }),
                            success: function (result) {
                                $.each(result.data,function (k, line) {
                                    var kind = line.kind;
                                    if(kind == 1) {
                                        htmldata += '<a href="#modal1" class ="collection-item waves-effect waves-light "><span class="badge ">下载</span>' + line.name + '</a> '
                                    }
                                    else{
                                        htmldata += '<a href="#top" class="collection-item avatar waves-effect waves-light"onclick="loadChildSvnDir(\'' + line.name + '\', \'' + line.path + '\')">' +
                                            '<i class="material-icons circle orange">folder</i>' + line.name + '</a> '
                                    }
                                })
                                $("#card-action").html('<b>' + htmltitle +'</b>');
                                $("#collection").html(htmldata);
                            },
                            error: function () {
                                alert('未找到路径');
                                return false
                            }
                        });
                        return false
                    }
                })
                return false
            }
        })
        if(htmltitle == '') {
            alert("未找到该分类文档");
            window.history.back(-1);
        }
    })
}


function loadChildSvnDir(title, path) {
    var htmldata = '';
    var backAction =  '';
    $("#card-action").html("目录读取中，请稍后……");
    $("#collection").html(htmldata);
    $.ajax({
        type: 'post',
        url: '/svnportal/svn/browser-out/',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: JSON.stringify({
            path: path
        }),
        success: function (result) {
            var arr = path.trim('/').split('/');
            var arrLenth = arr.length
            if(arrLenth <= 4) {
                backAction ='<i class="material-icons back-icon" onclick="location.href=\'list-details.html?dir=' + dir + '&code='+ code + '\'">返回 reply</i>';
            }
            else {
                var lastTitle = arr[arrLenth - 2];
                var lastPath = '';
                $.each(arr, function (i,item) {
                    if(i < arrLenth - 1 && item != '') {
                        lastPath += '/' + item;
                    }
                })
                backAction ='<i class="material-icons back-icon" onclick="loadChildSvnDir(\'' + lastTitle + '\', \'' + lastPath + '\')">返回 reply</i>';
            }
            if(result.data.length > 0) {
                $.each(result.data, function (k, line) {
                    var kind = line.kind;
                    if (kind == 1) {
                        htmldata += '<a href="#modal1" class="collection-item waves-effect waves-light "><span class="badge ">下载</span>' + line.name + '</a> '
                    }
                    else {
                        htmldata += '<a href="#top"  onclick="loadChildSvnDir(\'' + line.name + '\', \'' + line.path + '\', \'0\')" class="collection-item avatar waves-effect waves-light" ><i class="material-icons circle orange">folder</i>' + line.name + '</a> '
                    }
                })
                $("#collection").html(htmldata);
            }
            else {
                htmldata = '<a href="#" class="collection-item avatar waves-effect waves-light red-text"><span class="title">当前目录暂无文件</span></a>'
                $("#collection").html(htmldata);
            }
            $("#card-action").html('<b>' + title + '</b>');
            $("#back-action").html(backAction);
        },
        error: function () {
            alert('未找到路径');
            return false
        }
    });
}
